package com.szxy.controller.system;

import com.szxy.constants.Constants;
import com.szxy.domain.Menu;
import com.szxy.dto.MenuDto;
import com.szxy.service.MenuService;
import com.szxy.vo.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: zwz
 * @Date:2020/11/22 19:45
 * @Description:
 **/
@RestController
@RequestMapping("/system/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单及权限信息
     *
     * @return
     */
    @GetMapping("listAllMenus")
    public AjaxResult listAllMenus(MenuDto menuDto) {
        List<Menu> list = this.menuService.listAllMenus(menuDto);
        return AjaxResult.success(list);
    }

    /**
     * 查询菜单的下拉树
     *
     * @return
     */
    @GetMapping("selectMenuTree")
    public AjaxResult selectMenuTree() {
        MenuDto menuDto = new MenuDto();
        menuDto.setStatus(Constants.STATUS_TRUE);
        //只查询可用的
        return AjaxResult.success(this.menuService.listAllMenus(menuDto));
    }


    /**
     * 添加菜单和权限信息
     *
     * @return
     */
    @GetMapping("addMenu")
    public AjaxResult addMenu(MenuDto menuDto) {
        return AjaxResult.success(this.menuService.addMenu(menuDto));
    }

    /**
     * 根据ID查询菜单和权限信息
     *
     * @return
     */
    @GetMapping("getMenuById/{menuId}")
    public AjaxResult getMenuById(@PathVariable Long menuId) {
        return AjaxResult.success(this.menuService.getOne(menuId));
    }


    /**
     * 根据ID查询菜单和权限信息
     *
     * @return
     */
    @GetMapping("updateMenu")
    public AjaxResult updateMenu(MenuDto menuDto) {
        return AjaxResult.success(this.menuService.updateMenu(menuDto));
    }

    /**
     * 根据ID删除菜单和权限信息
     *
     * @return
     */
    @GetMapping("deleteMenuById/{menuId}")
    public AjaxResult deleteMenuById(@PathVariable Long menuId) {
        return AjaxResult.success(this.menuService.deleteMenuById(menuId));
    }


    /***
     * 根据角色 ID 查询菜单权限 ID 数据
     *
     * */
    @GetMapping("getMenuIdsByRoleId/{roleId}")
    public AjaxResult getMenuIdsByRoleId(@PathVariable Long roleId) {
        List<Long> ids = this.menuService.getMenusIdsByRoleId(roleId);
        return AjaxResult.success(ids);
    }

}
